<template>
  <el-row v-loading="loading">

    <el-col :span="24">
      <result-edit ref="resultEdit" v-on:save-finished="getResultList()"></result-edit>
<!--      <body-Edit ref="bodyEdit" v-on:save-finished="getTBodyList()"></body-Edit>-->
      <el-row class="search-row">
        <el-col :span="12">
          <el-input placeholder="姓名" size="small" v-model="search.name">
            <el-button slot="append" icon="el-icon-search" @click="getResultList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="12" style="text-align: right;">
          <el-button type="primary" icon="el-icon-plus" size="small"
                     v-show="permission.add" @click="handleCreateResult()">添加</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table :data="resultList" border size="mini" header-cell-class-name="header-row" style="text-align : center;">
            <el-table-column prop="tMemberId" label="档案号" header-align="center"></el-table-column>
            <el-table-column prop="name" label="姓名" header-align="center"></el-table-column>
            <el-table-column prop="ctime" label="心理测评日期" header-align="center"></el-table-column>
            <el-table-column prop="tOcd" label="强迫症化" header-align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.tOcd>=30">正常</span>
                <span v-else>异常</span>
              </template>
            </el-table-column>
            <el-table-column prop="tAnxiety" label="焦虑症化" header-align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.tAnxiety>=30">正常</span>
                <span v-else>异常</span>
              </template>
            </el-table-column>
            <el-table-column prop="tTerror" label="恐怖化" header-align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.tTerror>=30">正常</span>
                <span v-else>异常</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="260">
              <template slot-scope="scope">
                <el-button v-show="permission.edit" type="text"
                           @click="handleEditResult(scope.$index, scope.row)">编辑</el-button>
                <el-button v-show="permission.edit" type="text"
                           @click="handleReadResult(scope.$index, scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.currentPage"
            :page-sizes="[5, 10, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="resultTotal">
          </el-pagination>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
  import { listTMResult , getResultById} from '@/api/xy/result'
  import { listResourcePermission } from "@/api/resource"
  import ResultEdit from "@/components/result/resultEdit"

  export default {
    data() {
      return {
        loading: false,
        permission: {
          add: true,
          edit: true,
          remove: true
        },
        search: {
          name: '',
          offset: 0,
          limit: 5,
          currentPage:1
        },
        resultTotal: 0,
        resultList: []
      }
    },
    methods: {
      getResultList() {
        this.loading = true
        listTMResult(this.search)
          .then(response => {
            this.resultTotal = response.data.total
            this.resultList = response.data.rows
            this.loading = false
          })
          .catch(error => {
            console.log(error)
            this.loading = false
          })
        // alert(this.resultList)
      },
      handleSizeChange(val) {
        this.search.limit = val;
        this.search.offset = (this.search.currentPage - 1) * val
        this.getResultList()
      },


      handleCreateResult() {
        this.$refs.resultEdit.$emit('openAddResultDialog')
      },
      handleCurrentChange(val) {
        this.search.offset = (val - 1) * this.search.limit
        this.search.currentPage = val
        this.getResultList()
      },
      async pageInit() {
        this.loading = true
        try {
          let [listTMResultResp,listPermissionResp] = await Promise.all([
            listTMResult(this.search),
             listResourcePermission(this.$route.meta.routerId)
          ])
          this.resultTotal = listTMResultResp.data.total
          this.resultList = listTMResultResp.data.rows
        //   this.permission.add = listPermissionResp.data.find(item => {
        //   return item.resourcePermission === "result:add"
        // })
        // this.permission.edit = listPermissionResp.data.find(item => {
        //   return item.resourcePermission === "result:update"
        // })

          this.loading = false
        } catch(error) {
          this.outputError(error)
        }
        console.log(this.resultList);
      },
      handleEditResult(index, row) {
        this.loading = true
        getResultById(row.id)
          .then(response => {
            this.$refs.resultEdit.$emit('openEditResultDialog', response.data)
            this.loading = false
          })
          .catch(error => {
            this.outputError(error)
          })
      },
      handleReadResult(index, row){
        this.loading = true
        getResultById(row.id)
          .then(response => {
            this.$refs.resultEdit.$emit('openReadResultDialog', response.data)
            this.loading = false
          })
          .catch(error => {
            this.outputError(error)
          })
      },
      outputError(error) {
        console.log(error.response ? error.response : error.message)
        this.loading = false
        this.$message({
          showClose: true,
          message: '出错了，请按F12查看浏览器日志。',
          type: 'error'
        })
      }
    },
    components: { ResultEdit },
    created() {
      this.pageInit()
    }
  }
</script>

<style lang="scss" scoped>
  .search-row {
    padding-bottom: 8px;
    .el-input {
      width: 260px;
    }
  }

  .role-locked {
    color: red;
  }
  .role-not-locked {
    color: green;
  }
</style>
